//
//  RadioDetailViewController.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/10.
//  Copyright © 2015年 周莹. All rights reserved.
//

#import "RadioDetailViewController.h"
#import "DetailModel.h"
#import "DetailTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface RadioDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *musicTableView;
@property (strong, nonatomic) IBOutlet UIImageView *coverImageView;
@property (strong, nonatomic) IBOutlet UIImageView *avatarIamgeView;
@property (strong, nonatomic) IBOutlet UILabel *nickNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *tagsLabel;
@property (strong, nonatomic) IBOutlet UILabel *introLabel;
@property (strong, nonatomic) IBOutlet UILabel *tracksLabel;

@property(nonatomic,strong)NSMutableArray *dataArray2;
@property(nonatomic,strong)NSMutableArray *dataArray1;
@end

@implementation RadioDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self downLoadGiftDataFinish:^{
        
        [self .musicTableView  reloadData];
    }];
    
    self.musicTableView.dataSource = self;
    self.musicTableView.delegate = self;
    [self.musicTableView  registerNib:[UINib  nibWithNibName:@"DetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    self.avatarIamgeView.layer.cornerRadius = 20;
    self.avatarIamgeView.layer.masksToBounds = YES;
    
}






-(NSMutableArray *)dataArray1{

    if (!_dataArray1) {
        _dataArray1 = [NSMutableArray  array ];
    }

    return _dataArray1;
}


-(NSMutableArray *)dataArray2{

    if (!_dataArray2) {
        _dataArray2 = [NSMutableArray  array];
    }
    return _dataArray2;
}
-(void)downLoadGiftDataFinish:(void(^)())result{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSURL *url = [[NSURL alloc] init];
        url = [NSURL URLWithString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/20?position=%@&albumId=%@&isAsc=true&device=android&title=%%E7%%B2%%BE%%E9%%80%%89%%7C%%E6%%AD%%8C%%E5%%8D%%95&pageSize=20",self.albumId,self.position,self.albumId]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            //请求数据
            NSDictionary *albumDict = resultDict[@"album"];
            DetailModel *model1 = [[DetailModel alloc] init];
            model1.nickname= albumDict[@"nickname"];
            model1.coverLarge = albumDict[@"coverLarge"];
            model1.avatarPath = albumDict[@"avatarPath"];
            model1.tags = albumDict[@"tags"];
            model1.intro = albumDict[@"intro"];
            model1.tracks = albumDict[@"tracks"];
            NSLog(@"%ld", model1.tracks);
            self.nickNameLabel.text = model1.nickname;
            [self.coverImageView  sd_setImageWithURL:[NSURL URLWithString:model1.coverLarge]];
            [self.avatarIamgeView   sd_setImageWithURL:[NSURL  URLWithString:model1.avatarPath]];
            self.tagsLabel.text = model1.tags;
            self.introLabel.text = model1.intro;
            self.tracksLabel.text = [NSString stringWithFormat:@"共%@集", model1.tracks ];
            NSDictionary *tracksDict = resultDict[@"tracks"];
            NSArray *array = tracksDict[@"list"];
            for (NSDictionary *dict in array) {
                DetailModel  *model2 = [[DetailModel  alloc] init];
                [model2 setValuesForKeysWithDictionary:dict];
                [self.dataArray2 addObject:model2];
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                result();
            });
            
        }];
        
    });
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray2.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    DetailTableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"cell"];
    DetailModel  *model = self.dataArray2[indexPath.row];
    [cell  setCellDataWithModel:model];
    UIColor *color  = [[UIColor alloc] initWithRed:255.0/255 green:222.0/255 blue:249.0/255 alpha:1.0];
    cell.backgroundColor = color;

    return cell;
    

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;


}



@end
